<template>
  <div class="app-container">
    <el-form :model="queryParam" ref="queryForm" :inline="true" :rules="rules">
      <el-form-item label="试卷ID：" prop="paperId">
        <el-input v-model="queryParam.paperId" clearable></el-input>
      </el-form-item>
      <el-form-item label="年份：" prop="gradeLevel">
        <el-input v-model="queryParam.gradeLevel" clearable></el-input>
<!--        <el-select v-model="queryParam.gradeLevel" placeholder="年级" @change="levelChange" clearable>
          <el-option v-for="item in levelEnum" :key="item.key" :value="item.key" :label="item.value"></el-option>
        </el-select>-->
      </el-form-item>
      <el-form-item label="学科：" >
        <el-select v-model="queryParam.subjectId"  clearable>
          <el-option v-for="item in subjectAll" :key="item.id" :value="item.id" :label="item.categoryName" ></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="小学科：" >
        <el-select v-model="queryParam.subjectId"  clearable>
          <el-option v-for="item in subjectFilter" :key="item.id" :value="item.id" :label="item.name+' ( '+item.levelName+' )'"></el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item label="试卷类型：" >
        <el-select v-model="queryParam.paperType" placeholder="试卷类型" clearable>
          <el-option v-for="item in paperTypeEnum" :key="item.id" :value="item.id" :label="item.categoryName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">查询</el-button>
        <router-link :to="{path:'/exam/paper/edit'}" class="link-left">
          <el-button type="primary">添加</el-button>
        </router-link>
      </el-form-item>
    </el-form>
    <!--表格-->
    <el-table v-loading="listLoading" :data="tableData" border fit highlight-current-row style="width: 100%">
      <!-- 列定义：Id -->
      <el-table-column prop="paperId" label="试卷Id" width="90px"/>
      <!-- 列定义：学科，使用     formatter 函数进行格式化显示 -->
      <el-table-column prop="subjectName" label="学科" /> <!--:formatter="subjectFormatter" width="120px" />-->
      <!-- 列定义：名称 -->
      <el-table-column prop="paperTypeName" label="试卷类型" />
      <!---->
      <el-table-column prop="name" label="试卷名称"  />
      <el-table-column prop="gradeLevel" label="试卷年份"  />
      <el-table-column prop="score" label="试卷总分"  />
      <el-table-column prop="questionCount" label="题目数量"  />
      <el-table-column prop="suggestTime" label="建议时长"  />
      <el-table-column prop="createUser" label="创建人Id"  />
      <!-- 列定义：创建时间，宽度为 160px -->
      <el-table-column prop="createTime" label="创建时间" width="160px"/>
      <!-- 列定义：操作，包含编辑和删除按钮 -->
      <el-table-column  label="操作" align="center"  width="160px">
        <template slot-scope="{row}">
          <!-- 编辑按钮，点击后通过路由跳转到编辑页面，携带 id 参数 -->
          <el-button size="mini" @click="$router.push({path:'/exam/paper/edit',query:{id:row.paperId}})" >编辑</el-button>
          <!-- 删除按钮，点击后调用 deletePaper 方法删除当前行数据 -->
          <el-button size="mini" type="danger"  @click="deletePaper(row)" class="link-left">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParam.pageIndex" :limit.sync="queryParam.pageSize"
                @pagination="search"/>
  </div>
</template>

<script>
import { mapGetters, mapState, mapActions } from 'vuex'
import Pagination from '@/components/Pagination'
import examPaperApi from '@/api/examPaper'

export default {
  components: { Pagination },
  data () {
    return {
      queryParam: {
        paperId: null,
        gradeLevel: null,
        subjectId: null,
        paperType: null,
        pageIndex: 1,
        pageSize: 10
      },
      subjectFilter: null,
      listLoading: true,
      tableData: [],
      /** 用来保存所有的科目**/
      subjectAll: [],
      paperTypeEnum: [],
      total: 0,
      rules: {
        paperId: [
          { type: 'string', message: '试卷Id必须是数字', trigger: 'blur' },
          { validator: this.validateSendUserId, trigger: 'blur' }
        ],
        gradeLevel: [
          { type: 'string', message: '年份必须是数字', trigger: 'blur' },
          { validator: this.validateSendUserId, trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    // console.log(this.paperTypeEnum)
    this.initSubject()
    this.search()
    this.subjectAllsearch()
    this.paperTypesearch()
  },
  methods: {
    validateUserId (rule, value, callback) {
      if (value === null || value === '') {
        callback()
      } else {
        const numValue = Number(value)
        console.log(value)
        if (isNaN(numValue)) {
          callback(new Error('年份必须是数字'))
        } else if (numValue <= 0) {
          callback(new Error('年份必须大于0'))
        } else {
          callback()
        }
      }
    },
    validateSendUserId (rule, value, callback) {
      if (value === null || value === '') {
        callback()
      } else {
        const numValue = Number(value)
        if (isNaN(numValue)) {
          callback(new Error('试卷Id必须是数字'))
        } else if (numValue <= 0) {
          callback(new Error('试卷Id必须大于0'))
        } else {
          callback()
        }
      }
    },
    subjectAllsearch () {
      examPaperApi.subjectList().then(data => {
        const re = data.response
        this.subjectAll = re
        // console.log(re)
      })
    },
    paperTypesearch () {
      examPaperApi.paperType().then(data => {
        const re = data.response
        this.paperTypeEnum = re
      })
    },
    submitForm () {
      this.queryParam.pageIndex = 1
      this.$refs.queryForm.validate((valid) => {
        if (valid) {
          this.search()
        } else {
          return false
        }
      })
    },
    search () {
      this.listLoading = true
      // console.log(this.queryParam)
      examPaperApi.pageList(this.queryParam).then(data => {
        const re = data.response
        this.tableData = re.list
        this.total = re.total
        this.queryParam.pageIndex = re.pageNum
        this.listLoading = false
      })
    },
    deletePaper (row) {
      let _this = this
      examPaperApi.deletePaper(row.paperId).then(re => {
        console.log(row.paperId)
        if (re.code === 1) {
          _this.search()
          _this.$message.success(re.message)
        } else {
          _this.$message.error(re.message)
        }
      })
    },
    levelChange () {
      this.queryParam.subjectId = null
      this.subjectFilter = this.subjects.filter(data => data.level === this.queryParam.level)
    },
    subjectFormatter  (row, column, cellValue, index) {
      return this.paperTypeEnum(cellValue)
    },
    ...mapActions('exam', { initSubject: 'initSubject' })
  },
  computed: {
    ...mapGetters('enumItem', ['enumFormat']),
    ...mapState('enumItem', {
      levelEnum: state => state.user.levelEnum
      // paperTypeEnum: state => state.exam.examPaper.paperTypeEnum
    }),
    ...mapGetters('exam', ['subjectEnumFormat']),
    ...mapState('exam', { subjects: state => state.subjects })
  }
}
</script>
